﻿#include "mainwindow.h"
#include <QApplication>
#include "Logger.h"
#include "dbservice.h"
#include "appmonitortaskmanager.h"
#include <QTextCodec>
#include <QMessageBox>
#include <QDir>
#include "statusreportertask.h"
#include "reportstatusreq.h"

#include "SysCfg.h"
#include <QMessageBox>

#define LOG_FILE "log/watchDog.log"
#define APP_UPDATE_DIR "app_update"

int init()
{

    QString currentPath = QCoreApplication::applicationDirPath();
    QDir::setCurrent(currentPath);

    CreatDir(APP_UPDATE_DIR);

    Logger::sharedInstance()->cfg(LoggerLevelInfo,LOG_FILE, 20,20,LoggerModeOnlyBothFileAndQDebug);
    if(gDBService.openDB(gDBService.mainDataBase) == f_success)
    {
        LOG_INFO("try to open db success");
        gDBService.checkAndUpdateDB();
    }
    else
    {
        LOG_ERROR("try to open db failed, init fail")
        QMessageBox::warning(NULL, "提示", "打开本地数据库失败，初始化失败");
        return f_fail;
    }

    if(isSocketOnListenning(LISTEN_PORT))
    {
        LOG_ERROR("port:%d is on listen, can't start it", LISTEN_PORT);
        QMessageBox::warning(NULL, "提示", QString("端口号:") + int2QStr(LISTEN_PORT) + "已经被其他程序占用，初始化失败");
        return f_fail;
    }

    if(gAppMonitorTaskManager.loadTasksFromDB() == f_success)
    {
        LOG_INFO("load app monitor tasks from db success");
    }
    else
    {
        LOG_ERROR("try to load app monitor tasks from db failed");
        QMessageBox::warning(NULL, "提示", "load app monitor tasks from db failed");
        return f_fail;
    }


    LOG_INFO("init success");
    return f_success;
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    if(f_success != init())
    {
        LOG_ERROR("try to init failed, program will exist");
        return 1;
    }

    LOG_INFO("program start success");




    MainWindow w;
    w.show();



    return a.exec();
}
